package com.imooc.controller;

import com.github.pagehelper.PageInfo;
import com.imooc.constant.RedisConstant;
import com.imooc.pojo.Orders;
import com.imooc.pojo.Users;
import com.imooc.service.MyOrderService;
import com.imooc.utils.IMOOCJSONResult;
import com.imooc.utils.PagedGridResult;
import com.imooc.utils.RedisOperator;
import com.imooc.vo.UsersVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.io.File;
import java.util.List;
import java.util.UUID;

/**
 * @author gcq
 * @Create 2022-01-12
 */
@Controller
public class BaseController {

    @Autowired
    public MyOrderService myOrderService;

    @Autowired
    private RedisOperator redisOperator;

    public static final Integer COMMON_PAGE_SIZE = 10;

    public static final Integer PAGE_SIZE = 20;

    public static final String FOODIE_SHOPCART = "shopcart";

    /**
     * 微信支付成功 -> 支付中心 -> 当前平台
     *                      -> 回调通知的url
     */
//    public static final String payReturnUrl = "http://127.0.0.1:8088/orders/notifyMerchantOrderPaid";
    public static final String payReturnUrl = "http://m8nrmz.natappfree.cc/orders/notifyMerchantOrderPaid";
    /**
     * 支付中心的调用地址
     */
    public static final String paymentUrl = "http://payment.t.mukewang.com/foodie-payment/payment/createMerchantOrder";

    /**
     * File.separator 斜杠 根据操作系统确定
     */
    public static final String IMAGE_USER_FACE_LOCATION = File.separator + "workspaces" +
                                                          File.separator + "images" +
                                                          File.separator + "foodie" +
                                                          File.separator + "faces";


    /**
     * 获取分页数据
     * @param list
     * @param page
     * @return
     */
    protected PagedGridResult getPageResult(List<?> list, Integer page) {
        PageInfo<?> pageInfo = new PageInfo<>(list);
        PagedGridResult grid = new PagedGridResult();
        grid.setPage(page);
        grid.setRows(list);
        grid.setTotal(pageInfo.getPages());
        grid.setRecords(pageInfo.getTotal());
        return grid;
    }

    protected IMOOCJSONResult checkUserOrder(String userId, String orderId) {
        Orders orders = myOrderService.queryMyOrder(userId, orderId);
        if(orders == null) {
            return IMOOCJSONResult.errorMsg("订单不存在");
        }
        return IMOOCJSONResult.ok(orders);
    }
    /**
     * 转换 userVO
     * @param user 登录用户信息
     * @return 封装好的用户信息给到前端
     */
    public UsersVO convertUserVO(Users user) {
        String uniqueToken = UUID.randomUUID().toString().trim();
        redisOperator.set(RedisConstant.REDIS_USER_TOKEN + user.getId(),
                uniqueToken);

        UsersVO usersVO = new UsersVO();
        BeanUtils.copyProperties(user, usersVO);
        usersVO.setUserUniqueToken(uniqueToken);

        return usersVO;
    }
}